package com.yhd.controller;

import com.yhd.entity.dto.UserDto;
import com.yhd.entity.query.UserVO;
import com.yhd.entity.query.WxVO;
import com.yhd.entity.result.ResultVo;
import com.yhd.service.UserService;
import com.yhd.utils.AesUtils;
import com.yhd.utils.RedisUtils;
import com.yhd.utils.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@Api(value = "小程序用户相关")
@Slf4j
@RequestMapping(value = "/app/user")
@CrossOrigin
public class AppUserController extends WebBaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private RedisUtils redisUtils;

    @ApiOperation(value = "通过code获取openId", httpMethod = "GET",  notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name="wxcode",value = "wxlogin方法获取的code",required = true,paramType = "query",dataType = "String")
            })
    @GetMapping("/getWxOpenId")
    @ResponseBody
    public ResultVo<String> getOpenId(String wxcode) {
        String openId = userService.getOpenId(wxcode);
        return ResultVo.success(openId);
    }

    @ApiOperation(value = "获取微信手机号", httpMethod = "POST",  notes = "{\n" +
            "  \"encryptedData\": \"包括敏感数据在内的完整用户信息的加密数据\",\n" +
            "  \"iv\": \"加密算法的初始向量\",\n" +
            "  \"openId\": \"微信用户ID\"\n" +
            "}")
    @PostMapping("/getWxPhone")
    @ResponseBody
    public ResultVo<String> getWxPhone(@RequestBody WxVO wxVO) {
        String phone = userService.getWxPhone(wxVO);
        return ResultVo.success(phone);
    }

    @ApiOperation(value = "登录/注册", httpMethod = "POST", notes = "{\n" +
            "  \"openId\": \"微信openId与手机号必传其一\",\n" +
            "  \"invitedCode\": \"邀请码\",\n" +
            "  \"phone\": \"手机号\"\n" +
            "}")
    @PostMapping("/login")
    @ResponseBody
    public ResultVo login(@RequestBody UserVO userVO) {
        UserDto userDto = userService.customerLogin(userVO);
        if (null != userDto){
            if (null != userDto.getState() && userDto.getState() == -1){
                return ResultVo.failure("10004","手机号已被绑定。");
            }else  if (null != userDto.getState() && userDto.getState() == -2){
                return ResultVo.failure("10004","微信号已被绑定。");
            }
            String token = null;
            try {
                token = TokenUtils.token(userVO.getLoginId(), AesUtils.encrypt(AesUtils.DEFAULT_PWS,"abc1234"));
            } catch (Exception e) {
                e.printStackTrace();
            }
            redisUtils.set("customer-"  + token, userDto.getId(),604800L);
            userDto.setAuthToken(token);
            return ResultVo.success(userDto);
        }
        return ResultVo.failure("10001","登录失败。");
    }

}
